/* eslint-disable prettier/prettier */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Injectable } from '@nestjs/common';
import { CreateDoctorDto } from './dto/create-doctor.dto';
import { UpdateDoctorDto } from './dto/update-doctor.dto';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Doctor } from './schemas/doctor.schema';

@Injectable()
export class DoctorsService {
  constructor(@InjectModel(Doctor.name) private doctorModel: Model<Doctor>) { }

  create(createDoctorDto: CreateDoctorDto) {
    return this.doctorModel.create(createDoctorDto)
  }

  findAll() {
    return this.doctorModel.find()
  }

  async findOne(id: string) {
    const data = await this.doctorModel.findOne({ _id: id }).exec();
    return {
      code: 200,
      msg: "查询成功！",
      data
    }
  }

  // 按医生姓名和擅长病症查询
  async search(input: string) {
    if (input.trim() != "") {
      const result = await this.doctorModel.find({
        $or: [
          { name: { $regex: input.trim() } },
          { specialties: { $regex: input.trim() } }
        ]
      })
      if (result.length > 0) {
        return {
          code: 200,
          msg: "搜索成功!",
          data: result
        }
      } else {
        return {
          code: 200,
          msg: "暂无搜索结果!"
        }
      }
    } else {
      return {
        code: 200,
        msg: "搜索参数不能为空!"
      }
    }
  }

  // update(id: number, updateDoctorDto: UpdateDoctorDto) {
  //   return `This action updates a #${id} doctor`;
  // }

  // remove(id: number) {
  //   return `This action removes a #${id} doctor`;
  // }
}
